<?php

namespace app\admin\model\card;

use think\Model;
use function fast\e;


class Carduser extends Model
{

    


    // 表名
    protected $name = 'view_card_user';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'card_type_text',
        'status_text',
        'starttime_text',
        'endtime_text',
        'addtime_text',
        'updatedtime_text'
    ];

    public $id = '';
    public $card_id = '';
    public $user_id = '';
    public $card_type = '';
    public $status = '';
    public $day_count = '';
    public $day_now = '';
    public $day_no = '';
    public $starttime = '';
    public $endtime = '';
    public $addtime = '';
    public $updatedtime = '';

    
    public function getCardTypeList()
    {
        return ['1' => __('Card_type 1'), '2' => __('Card_type 2')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getCardTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['card_type']) ? $data['card_type'] : '');
        $list = $this->getCardTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStarttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getAddtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['addtime']) ? $data['addtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdatedtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['updatedtime']) ? $data['updatedtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStarttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setAddtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdatedtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function card()
    {
        return $this->belongsTo('app\admin\model\view\Card', 'card_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function add(){
        $cardInfo = (new Card())->find($this->card_id);
        if (empty($cardInfo)){
            return false;
        }



        $day_count = $cardInfo['day'];

        $currentTimestamp = time();
        // 加上时间戳
        $futureTimestamp = strtotime('+'.$day_count.' days', $currentTimestamp);
        // 将时间戳格式化为日期
        $futureDate = date('Y-m-d H:i:s', $futureTimestamp);

        $this->starttime = $currentTimestamp;
        $this->day_count = $day_count;

        $data = [
            'card_id' => $this->card_id,
            'user_id' => $this->user_id,
            'card_type' => $this->card_type,
            'day_count' => $this->day_count,
            'day_no' => $this->day_count,
            'starttime' => $currentTimestamp,
            'endtime' => $futureTimestamp,
            'addtime' => $currentTimestamp,
            'updatedtime' => $currentTimestamp,
        ];


        $result = $this->insert($data);
        return  $result;
    }

}
